package com.rytx.system.service.impl;

import com.rytx.common.utils.DateUtils;
import com.rytx.system.domain.ysp.ArtFundingDetails;
import com.rytx.system.domain.ysp.ArtOrder;
import com.rytx.system.mapper.ArtFundingDetailsMapper;
import com.rytx.system.service.IArtFundingDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArtFundingDetailsServiceImpl implements IArtFundingDetailsService {
    @Autowired
    private ArtFundingDetailsMapper artFundingDetailsMapper;

    @Override
    public int insertArtFundingDetails(ArtFundingDetails artFundingDetails) {
        return artFundingDetailsMapper.insert(artFundingDetails);
    }

    @Override
    public List<ArtFundingDetails> selectArtFundingDetailsStatisticsByParam(ArtFundingDetails artFundingDetails) {
        return artFundingDetailsMapper.selectArtFundingDetailsStatisticsByParam(artFundingDetails);
    }

    @Override
    public List<ArtFundingDetails> detailList(ArtFundingDetails artFundingDetails) {
        return artFundingDetailsMapper.selectArtFundingDetailsList(artFundingDetails);
    }

    @Override
    public Map<String, BigDecimal> getYearIncome() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusMonths(12).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,BigDecimal> map = new HashMap<>();
        List<ArtFundingDetails> artFundingDetails = artFundingDetailsMapper.selectArtFundingDetailsListInDate(startDate, endDate,"1");
        for (ArtFundingDetails fundingDetails : artFundingDetails) {
            String k = fundingDetails.getStatisticsMonth();
            BigDecimal val = map.get(k);
            map.put(k, val == null? BigDecimal.ZERO : val.add(fundingDetails.getFundingAmount()));
        }
        return map;
    }

    @Override
    public Map<String, BigDecimal> getMonthIncome() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusDays(30).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,BigDecimal> map = new HashMap<>();
        List<ArtFundingDetails> artFundingDetails = artFundingDetailsMapper.selectArtFundingDetailsListInDate(startDate, endDate,"1");
        for (ArtFundingDetails fundingDetails : artFundingDetails) {
            String k = fundingDetails.getStatisticsDay();
            BigDecimal val = map.get(k);
            map.put(k, val == null? BigDecimal.ZERO : val.add(fundingDetails.getFundingAmount()));
        }
        return map;
    }

    @Override
    public Map<String, BigDecimal> getWeekIncome() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusDays(7).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,BigDecimal> map = new HashMap<>();
        List<ArtFundingDetails> artFundingDetails = artFundingDetailsMapper.selectArtFundingDetailsListInDate(startDate, endDate,"1");
        for (ArtFundingDetails fundingDetails : artFundingDetails) {
            String k = fundingDetails.getStatisticsDay();
            BigDecimal val = map.get(k);
            map.put(k, val == null? BigDecimal.ZERO : val.add(fundingDetails.getFundingAmount()));
        }
        return map;
    }

    @Override
    public Map<String, BigDecimal> getWeekPayout() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusDays(7).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,BigDecimal> map = new HashMap<>();
        List<ArtFundingDetails> artFundingDetails = artFundingDetailsMapper.selectArtFundingDetailsListInDate(startDate, endDate,"2");
        for (ArtFundingDetails fundingDetails : artFundingDetails) {
            String k = fundingDetails.getStatisticsDay();
            BigDecimal val = map.get(k);
            map.put(k, val == null? BigDecimal.ZERO : val.add(fundingDetails.getFundingAmount()));
        }
        return map;
    }

    @Override
    public Map<String, BigDecimal> getMonthPayout() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusDays(30).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,BigDecimal> map = new HashMap<>();
        List<ArtFundingDetails> artFundingDetails = artFundingDetailsMapper.selectArtFundingDetailsListInDate(startDate, endDate,"2");
        for (ArtFundingDetails fundingDetails : artFundingDetails) {
            String k = fundingDetails.getStatisticsDay();
            BigDecimal val = map.get(k);
            map.put(k, val == null? BigDecimal.ZERO : val.add(fundingDetails.getFundingAmount()));
        }
        return map;
    }

    @Override
    public Map<String, BigDecimal> getYearPayout() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusMonths(12).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,BigDecimal> map = new HashMap<>();
        List<ArtFundingDetails> artFundingDetails = artFundingDetailsMapper.selectArtFundingDetailsListInDate(startDate, endDate,"2");
        for (ArtFundingDetails fundingDetails : artFundingDetails) {
            String k = fundingDetails.getStatisticsMonth();
            BigDecimal val = map.get(k);
            map.put(k, val == null? BigDecimal.ZERO : val.add(fundingDetails.getFundingAmount()));
        }
        return map;
    }
}
